package org.javaforever.gatescore.utils;

import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.javaforever.gatescore.core.FrontDomain;
import org.javaforever.gatescore.core.FrontField;
import org.javaforever.gatescore.exception.ValidateException;

public class DomainUtil {
	public static FrontDomain findDomainInSet(Set<FrontDomain> targets, String domainName) throws ValidateException{
		for (FrontDomain d: targets){
			if (d.getStandardName().equals(domainName)) return d;
		}
		throw new ValidateException("域对象"+domainName+"不在集合中！");
	}
	
	public static Boolean inDomainSet(FrontDomain d,Set<FrontDomain> set){
		for (FrontDomain dn :set){
			if (d.getStandardName().equals(dn.getStandardName())){
				return true;
			}
		}
		return false;
	}
	
	public static FrontDomain findDomainInList(List<FrontDomain> targets, String domainName) throws ValidateException{
		for (FrontDomain d: targets){
			if (d.getStandardName().equals(domainName)) return d;
		}
		throw new ValidateException("域对象"+domainName+"不在列表中！");
	}
	
	public static Boolean setContainsDomain(Set<FrontDomain> set, String domainName) {
		for (FrontDomain d : set) {
			if (d.getStandardName().equals(domainName))
				return true;
		}
		return false;
	}
	
	public static Boolean listContainsDomain(List<FrontDomain> set, String domainName) {
		for (FrontDomain d : set) {
			if (d.getStandardName().equals(domainName))
				return true;
		}
		return false;
	}
	
	public static FrontDomain lookupDoaminInSet(Set<FrontDomain> set, String domainName) {
		for (FrontDomain d : set) {
			if (d.getStandardName().equals(domainName)&&!d.isLegacy())
				return d;
		}
		return null;
	}
	
	public static FrontDomain lookupDoaminInList(List<FrontDomain> set, String domainName) {
		for (FrontDomain d : set) {
			if (d.getStandardName().equals(domainName)&&!d.isLegacy())
				return d;
		}
		return null;
	}
	
	public static FrontDomain findDomainInListOrReturnNull(List<FrontDomain> targets, String domainName){
		for (FrontDomain d: targets){
			if (d.getStandardName().equals(domainName)) return d;
		}
		return null;
	}
	
	public static boolean validateId(String val) {
		if (StringUtil.isBlank(val)) return true;
		boolean flag = false;
		try {
			String regex = "^([0]|[1-9][0-9]*)$";
			Pattern p = Pattern.compile(regex);
			Matcher m = p.matcher(val);
			if (m.find()) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public static FrontField findDomainFieldByFieldName(FrontDomain d,String fieldName) {
		for (FrontField f:d.getAllFields()) {
			if (f.getFieldName().equals(fieldName)) return f;
		}
		return null;
	}

	public static String DomainSetNamesWithQuote(Set<FrontDomain> mydomains) {
		if (mydomains == null || mydomains.size()==0) return null;
		StringBuilder sb = new StringBuilder("");
		for (FrontDomain fd:mydomains) {
			sb.append("\"").append(fd.getStandardName()).append("\",");
		}
		return sb.substring(0,sb.length()-1);
	}
	
	public static String DomainSetNamesWithSingleQuote(Set<FrontDomain> mydomains) {
		if (mydomains == null || mydomains.size()==0) return null;
		StringBuilder sb = new StringBuilder("");
		for (FrontDomain fd:mydomains) {
			sb.append("'").append(fd.getStandardName()).append("', ");
		}
		return sb.substring(0,sb.length()-2);
	}	
}
